package com.dhh.sky.activity;

import java.io.File;
import java.util.List;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
import com.dhh.sky.R;
import com.dhh.sky.util.Constant;
import com.dhh.sky.widget.FilePickActionMode;
import com.dhh.sky.widget.FilePickerListAdapter;

public class FilePickActivity extends SherlockFragmentActivity {
	private ActionMode mMode;
	private ListView listView;
	private FilePickerListAdapter adapter;
	private boolean isAMActive = false;
	private String path;
	
	@Override 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
        setContentView(R.layout.file_choose); 
        
        path = this.getIntent().getExtras().getString(Constant.PARAM_PATH);
        
        initActionBar();
        initView();
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem cancelItem = menu.add(getString(R.string.label_cancel));
		cancelItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		cancelItem.setOnMenuItemClickListener(new OnMenuItemClickListener(){
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				setResult(-1, null);
				finish();
				return true;
			}});
		
		return true;
	}
	
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
        	if(adapter.hasParent()){
                adapter.setFolder(adapter.getFolder().getParentFile());
                initData();
        	}
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
	
	public void initView(){
		listView = (ListView) this.findViewById(R.id.file_list);
		adapter = new FilePickerListAdapter(this);
		adapter.setFolder(Environment.getExternalStorageDirectory());
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				File file = adapter.getItem(position);
				if(file.isDirectory()){						
					adapter.setFolder(file);
					initData();
				}else{
					adapter.selectFile(view,position);
				}
			}
		});
		
		initData();
	}
	
	public void initData(){
		adapter.loadFile();
		adapter.notifyDataSetChanged();
		getSupportActionBar().setTitle(adapter.getFolder().getAbsolutePath());
	}
	
	protected void initActionBar(){
    	getSupportActionBar().setHomeButtonEnabled(false);     
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);   
        getSupportActionBar().setDisplayShowHomeEnabled(false);
	}
	
	public void modeChanged(){
		if(adapter.getSelectCount()>0 && !isAMActive){	
			mMode = this.startActionMode(new FilePickActionMode(this));
			isAMActive = true;		
		}else if(!(adapter.getSelectCount()>0)){ 
			if (mMode != null) {
                mMode.finish();
                isAMActive = false;
            }
		}
		
		if(mMode!=null){
			mMode.setTitle("Selected "+adapter.getSelectCount()+" File(s)");
		}
	}
	
	public void unSelectFile(){
		adapter.unSelectFiles();
		isAMActive = false;
	}
	
	public String[] getItemNameSelected(){
		List<File> files = adapter.getItemSelected();
		String[] names = new String[files.size()];
		for(int i=0; i<names.length; i++){
			names[i] = files.get(i).getAbsolutePath();
		}
		return names;
	}
	
	public String getTargetPath(){
		return path;
	}
}
